import httpInstance from "@/utils/http";

// 用户密码登录
export async function passwordLoginAPI({ phone, password }) {
  try {
    const res = await httpInstance.request({
      url: '/api/login/password',
      method: 'POST',
      data: {
        phone,
        password
      }
    })
    return res
  } catch (error) {
    throw error;
  }
}

// 邮箱验证码登录
export async function emailLoginAPI({ email, code }) {
  try {
    const res = await httpInstance.request({
      url: '/api/login/email',
      method: 'POST',
      data: {
        email,
        code
      }
    })
    return res
  } catch (error) {
    throw error;
  }
}

// 启用或禁用用户
export async function enableUserAPI(e) {
  try {
    const res = await httpInstance.request({
      url: '/api/user/enable',
      method: 'POST',
      data: {
        email:e,
        enable :true
      }
    })
    return res
  } catch (error) {
    throw error;
  }
}
export async function czBalance({ balance }) {
  try {
    const res = await httpInstance.request({
      url: '/api/cz',
      method: 'POST',
      data: {
        balance,
      }
    })
    return res
  } catch (error) {
    throw error;
  }
}

// 登出
export async function logoutAPI() {
  try {
    const res = await httpInstance.request({
      url: '/api/logout',
      method: 'POST',

      headers: {
        'Authorization': localStorage.getItem('token')
      }
    })
    return res.data
  } catch (error) {
    throw error;
  }
}

// 用户注册
export async function registerAPI({ userName,email,phone, password, nickname }) {
  try {
    const res = await httpInstance.request({
      url: '/api/register',
      method: 'POST',
      data: {
        userName ,
        email,
        phone,
        password,
        nickname
      }
    })
    return res
  } catch (error) {
    throw error;
  }
}

// 获取邮箱验证码
export async function getEmailCodeAPI({email,name}) {
  try {
    const res = await httpInstance.request({
      url: '/api/email/getCode',
      method: 'POST',
      params: {
        email,
        name
      }
    })
    return res
  } catch (error) {
    throw error;
  }
}

// 验证邮箱
export async function verifyEmailAPI(e,c) {
  try {
    const res = await httpInstance.request({
      url: '/api/email/verify',
      method: 'POST',
      data: {
        email: e,
        code: c
      }
    })
    return res
  } catch (error) {
    throw error;
  }
}

// 重置密码
export async function resetPasswordAPI(e) {
  try {
    const res = await httpInstance.request({
      url: '/api/email/resetPassword',
      method: 'POST',
      data: {
        email:e,
      }
    })  
    return res
    }
   catch (error) {
      throw error;
    }
}

// 获取用户列表
export async function getUserListAPI(current=1,size=10) {
  try {
    const res = await httpInstance.request({
      url: '/api/user/list',
      method: 'POST',
      data: {
        "current": current,
        "size": size
    }
    })
    return res 
  } catch (error) {
    throw error;
  }
  
}